package com.maimao.search.controller;

import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.maimao.core.constant.Constants;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.PageResult;
import com.maimao.core.model.resp.Result;
import com.maimao.core.utils.DateUtils;
import com.maimao.model.goods.enums.GoodsCheckEnum;
import com.maimao.model.goods.log.GoodsCheck;
import com.maimao.model.goods.log.GoodsCheckInput;
import com.maimao.search.controller.base.BaseController;
import com.maimao.search.service.GoodsCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author MaoLin Wang
 * @date 2021/3/22 2:18 下午
 */
@RequestMapping("/goodsCheck")
@RestController
public class GoodsCheckController extends BaseController {

    @Autowired
    private GoodsCheckService goodsCheckService;

    @PostMapping("/save")
    public Result<?> save(@RequestBody @Validated GoodsCheck goodsCheck) {
        if (ObjectUtil.isNull(goodsCheck.getDeliveryTime())) {
            goodsCheck.setDeliveryTime(DateUtils.now());
        }
        goodsCheckService.saveOrUpdate(goodsCheck);
        return Result.ok();

    }

    @PostMapping("/updateStatus")
    public Result<?> updateStatus(@RequestBody GoodsCheck goodsCheck) {
        if (goodsCheck.getStatus() == null || goodsCheck.getId() == null || Long.valueOf(goodsCheck.getId()) < 1) {
            return Result.notValid("status and id can not be null");
        }
        if (ObjectUtil.isNull(goodsCheck.getDeliveryTime())) {
            goodsCheck.setCheckTime(DateUtils.now());
        }
        boolean res = goodsCheckService.update(goodsCheck);
        return res ? Result.ok("更新成功") : Result.fail("更新失败");

    }

    /**
     * 商品服务端内部调用
     *
     * @param checkInput
     * @return
     */
    @PostMapping("/list")
    public Result<?> list(@RequestBody GoodsCheckInput checkInput) {
        if (StrUtil.isBlank(checkInput.getSysUserId())) {
            return Result.notValid();
        }
        if (checkInput.getPage() == null || checkInput.getPage() < 1) {
            checkInput.setPage(1);
        }
        if (checkInput.getSize() == null || checkInput.getSize() < 1) {
            checkInput.setSize(Constants.TEN);
        }
        if (checkInput.getGoodsName() != null) {
            checkInput.setGoodsName(checkInput.getGoodsName().trim());
        }
        if (checkInput.getFromTime() != null && checkInput.getEndTime() != null && checkInput.getFromTime().compareTo(checkInput.getEndTime()) > 0) {
            return Result.notValid("非法日期范围");
        }
        PageResult<GoodsCheck> result = goodsCheckService.query(checkInput);
        return Result.okData(result);
    }


}
